import socket
import wifisocket


class socket_int:

    def __init__(self,host):
        print("python-socket----------------------------------------------"+str(socket.AF_INET))
        # 创建 socket 对象
        # 创建 socket 对象
        print('{0} is a legal ip address!'.format(host))


        host = host
        port = 8080
        print("Opening connection to {}:{}...".format(host, port))
        self.client_socket = socket.socket()
        # self.client_socket.setsockopt(socket.IPPROTO_TCP,socket.TCP_NODELAY,True)

        try:
            self.client_socket.connect((host, int(port)))
        except socket.error as msg:
            print('WARNING: {}'.format(msg))
            return

        print('Connected')
        self.client_socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        self.socket_connect = wifisocket.socket_connect(self.client_socket)
        print("python-socket----------------------------------------------")

    def read(self):
        # 接收小于 1024 字节的数据
        msg = self.socket_connect.read()
        #s.close()
        print("python-socket----------------------------------------------")
        print (msg.decode('utf-8'))
        return msg

